<?php

namespace App\Services;

use App\Models\GrabOrderDelivery;
use App\Models\Order;
use PHPUnit\Exception;

/**
 * Class GrabService
 * @package App\Services
 * 2021/3/19 - 13:45
 * Administrator
 * 抢单
 */
class GrabService extends BaseService
{


    public $model;
    public $orderModel;

    /**
     * GrabService constructor.
     */
    public function __construct()
    {
        $this->model = new GrabOrderDelivery();
        $this->orderModel = new Order();
    }


    /**
     * 2021/3/19--13:51
     * @author:董宗澳
     * @email:2453611300
     * @wechat:DZA74941
     * @QQ:2453611300
     * @web:www.quzha.net
     * @method:getList
     * @effect:获取抢单列表
     */
    public function getList()
    {
        $userService = new UserService();
        $user_info = $userService->getUserInfo('estate');
        try {
            $list = $this->model->with(['order' => function ($query) {
                return $query->with(['store' => function ($query) {
                    return $query->select('id', 'store_name', 'store_logo');
                }, 'user' => function ($query) {
                    return $query->select('id', 'username');
                }, 'order_goods']);
            }]);
            if (isset(request()->created_at[0]) && isset(request()->created_at[1])) {
                $start = strtotime(request()->created_at[0]);
                $end = strtotime(request()->created_at[1]);
                $list->whereBetween('grab_time', [$start,$end]);
            }
            if (isset(request()->user_id) && !empty(request()->user_id)) {
                $list->where('delivery_id', request()->user_id);
            }
            if ($user_info['pid'] !== 0) {
                $list->where('delivery_id', $user_info['id']);
            }
            $list = $list->paginate($request->per_page ?? 30);
            return $this->format($list);
        } catch (Exception $e) {
            return $this->format_error($e->getMessage());
        }
    }


    /**
     * 2021/3/19--13:49
     * @author:董宗澳
     * @email:2453611300
     * @wechat:DZA74941
     * @QQ:2453611300
     * @web:www.quzha.net
     * @method:add
     * @effect:添加抢单
     */
    public function add()
    {
        try {
            $userService = new UserService();
            $userInfo = $userService->getUserInfo('estate');
            $id = request()->id;
            $data = [
                'order_id' => $id,
                'delivery_id' => $userInfo['id'],
                'grab_time' => now()->timestamp,
                'stop' => 1
            ];
            $this->model->insert($data);
            $order = $this->orderModel->where('id', $id)->update(['grab_type' => 1]);
            return $this->format([]);
        } catch (\Exception $e) {
            return $this->format_error($e->getMessage());
        }
    }


    /**
     * 2021/3/20--17:57
     * @author:董宗澳
     * @email:2453611300
     * @wechat:DZA74941
     * @QQ:2453611300
     * @web:www.quzha.net
     * @method:changeStatus
     * @effect:修改状态
     */
    public function changeStatus($id, $stop)
    {
        return $this->model->whereIn('id', $id)->update(['stop' => $stop]);
    }
}
